package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.service.wx.WxCouponService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.wx.lhh.CouponExchangeVo;
import com.cskaoyan.market.vo.wx.lyt.CouponListVo;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: WxCouponController
 * @Auther: liz
 * @Date: 2023/05/22/17:11
 * @Description:
 */
@RestController
@RequestMapping("/wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService wxCouponService;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @GetMapping("/list")
    public Object list() {
        PageHelper.startPage(1, 10);
        List<CouponListVo> coupons = wxCouponService.list();
        Object o = ResponseUtil.okList(coupons);
        return o;
    }

    @PostMapping("/receive")
    //
    public Object receive(@RequestBody Map<String, Integer> map) {
        Integer couponId = map.get("couponId");
        Integer i = wxCouponService.receiveCoupon(couponId);
        if (i == 1) {
            return ResponseUtil.fail(740, "优惠券已领取过");
        }
        if (i == 2) {
            return ResponseUtil.fail(740, "优惠券已领完");
        }
        if (i == 0) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @GetMapping("/mylist")
    public Object mylist(Short status, Integer page, Integer limit) {
        // status=0&page=1&limit=10
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");

        List<CouponListVo> result = wxCouponService.mylist(status, page, limit, userId);
        return ResponseUtil.okList(result);

    }

    @GetMapping("/selectlist")
    public Object selectList(Integer cartId, Integer grouponRulesId) {
        //cartId=546&grouponRulesId=0
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        List<CouponExchangeVo> result = wxCouponService.selectList(cartId, grouponRulesId, userId);
        return ResponseUtil.okList(result);
    }

    @PostMapping("exchange")
    public Object exchange(@RequestBody Map<String, String> map) {
        String code = map.get("code");
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        Integer couponId = wxCouponService.exchange(code, userId);
        if (couponId == null) {
            return ResponseUtil.fail(740, "兑换码不存在");
        }
        Integer row = wxCouponService.receiveCoupon(couponId);
        if (row == 1) {
            return ResponseUtil.fail(740, "优惠券已领取过");
        }
        if (row == 2) {
            return ResponseUtil.fail(740, "优惠券已领完");
        }
        if (row == 0) {
            return ResponseUtil.ok();
        }

        return ResponseUtil.fail();
    }

}
